package cn.xllyd.web.controller;

import cn.xllyd.domain.Order;
import cn.xllyd.service.IOrderService;
import cn.xllyd.query.OrderQuery;
import cn.xllyd.result.JSONResult;
import cn.xllyd.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    public IOrderService orderService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Order order){
        if(order.getId()!=null){
            orderService.updateById(order);
        }else{
            orderService.insert(order);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        orderService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(orderService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(orderService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody OrderQuery query){
        Page<Order> page = new Page<Order>(query.getPage(),query.getRows());
        page = orderService.selectPage(page);
        return JSONResult.success(new PageList<Order>(page.getTotal(),page.getRecords()));
    }

    /**
     * 下单
     */
    @RequestMapping(value="/inOrder",method= RequestMethod.POST)
    public JSONResult inOrder(@RequestBody Order order){

        return JSONResult.success(orderService.inOrder(order));
    }

    /**
     * 接单
     */
    @RequestMapping(value = "/getOrder/{orderNum}",method = RequestMethod.GET)
    public JSONResult getOrder(@PathVariable("orderNum")String orderNum){
        return JSONResult.success(orderService.getOrder(orderNum));
    }

}
